#include <utility>
#include "Node.h"

using namespace std;

void print_tree(Node* root) {
    if(root) {
        if(root->is_leaf()) {
            printf("[%d *%d]", root->get_image(), root->get_father());
        } else {
            printf("( ");
            print_tree(root->get_left());
            printf(" %s *%d ", root->get_orientation() == HORIZONTAL ? "H" : "V", root->get_father());
            print_tree(root->get_right());
            printf(" )");
        }
    }
}


pair<double, double>* node_ea(Node* node) {
    pair<double, double>* EA;
    if(node->is_leaf()) {
        EA = new pair<double, double>(node->get_image()->get_relative_area(), node->get_image()->get_aspect_ratio());
    } else {
        pair<double, double>* leftEA = node_ea(node->get_left());
        pair<double, double>* rightEA = node_ea(node->get_right());
        double relativeArea;
        double aspectRatio;
        if(node->get_orientation() == VERTICAL) {
            double maxRelativeHeight = max(sqrt(leftEA->first*leftEA->second), sqrt(rightEA->first*rightEA->second));
            double totalRelativeWidth = sqrt(leftEA->first / leftEA->second) + sqrt(rightEA->first / rightEA->second);
            relativeArea = maxRelativeHeight * totalRelativeWidth;
            aspectRatio = maxRelativeHeight / totalRelativeWidth;
        } else {
            double totalRelativeHeight = sqrt(leftEA->first * leftEA->second) + sqrt(rightEA->first * rightEA->second);
            double maxRelativeWidth = max(sqrt(leftEA->first / leftEA->second), sqrt(rightEA->first / rightEA->second));
            relativeArea = maxRelativeWidth * totalRelativeHeight;
            aspectRatio = totalRelativeHeight / maxRelativeWidth;
        }
        EA = new pair<double, double>(relativeArea, aspectRatio);
    }
    return EA;
}


